package com.alibaba.algo.apiimpl.user;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercomponent.service.user.SysEmployeeGroupService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SysEmployeeGroupDeleteRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SysEmployeeGroupPageRequestVO;
import com.alibaba.algo.core.base.PagerEntity;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.domain.user.SysEmployeeGroupDomain;
import com.alibaba.algo.entity.user.SysEmployeeGroup;
import com.alibaba.algo.exception.BaseErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 类SysEmployeeGroupServiceImpl.java的实现描述：用户组关系表 serviceimpl
 *
 * @author fan.tianpeng 2021-05-06 20:30:48
 */
@Slf4j
@RestController
public class SysEmployeeGroupServiceImpl implements SysEmployeeGroupService {

    @Autowired
    private SysEmployeeGroupDomain sysEmployeeGroupDomain;

    /**
     * 查询用户组下的员工列表
     *
     * @param vo 入参
     * @return 员工列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult selectEmployeeGroupPage(SysEmployeeGroupPageRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getPageIndex())) {
            throw new BaseException(BaseErrorCode.PAGE_INDEX_IS_NULL.getCode(), BaseErrorCode.PAGE_INDEX_IS_NULL.getMessage());
        }
        if (ObjectUtils.isEmpty(vo.getPageSize())) {
            throw new BaseException(BaseErrorCode.PAGE_SIZE_IS_NULL.getCode(), BaseErrorCode.PAGE_SIZE_IS_NULL.getMessage());
        }
        if (ObjectUtils.isEmpty(vo.getGroupId())) {
            throw new BaseException(BaseErrorCode.GROUP_ID_IS_NULL.getCode(), BaseErrorCode.GROUP_ID_IS_NULL.getMessage());
        }
        Long groupId = vo.getGroupId();
        SysEmployeeGroup sysEmployeeGroup = SysEmployeeGroup.builder().build();
        sysEmployeeGroup.setGroupId(groupId);
        PagerEntity pagerEntity = sysEmployeeGroupDomain.pageByPara(sysEmployeeGroup, vo.getPageIndex(), vo.getPageSize(), vo.getSortMap());
        return BaseResult.buildSuccess(pagerEntity);
    }

    /**
     * 删除用户组的员工
     *
     * @param vo 入参
     * @return 删除结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public BaseResult singleDelete(SysEmployeeGroupDeleteRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getEmployeeGroupId())) {
            throw new BaseException(BaseErrorCode.EMPLOYEE_GROUP_ID_IS_NULL.getCode(), BaseErrorCode.EMPLOYEE_GROUP_ID_IS_NULL.getMessage());
        }
        SysEmployeeGroup sysEmployeeGroup = SysEmployeeGroup.builder().build();
        sysEmployeeGroup.setId(vo.getEmployeeGroupId());
        sysEmployeeGroup.setIsDeleted(YesOrNo.Y.name());
        sysEmployeeGroup.setGmtModified(LocalDateTime.now());
        if(null != vo && null != vo.getEmployeeGroupId() && vo.getEmployeeGroupId() > 0){
            sysEmployeeGroupDomain.updateById(sysEmployeeGroup);
        }
        return BaseResult.buildSuccess();
    }
}
